import { ImageSource } from "pixi.js";
import { ResourceLoader } from "@/utils/ResourceLoader.js";
import { ImageSourceRefQueue } from "@/utils/ImageSource/ImgSrcRefQueue.js";
import { ImgResidentQueue } from "@/utils/ImageSource/imgResidentQueue.js";

export class ImageManager {
    static imageSources = {};
    static imageTaskCount = 0;

    static isResident(floder) {return floder == 'system' || floder == 'resident';}
    static isReady() {return this.imageTaskCount == 0;}

    static async loadImageSource(floder, filename) {
        if(!filename)return null;
        let group = this.imageSources[floder];
        if (!group) {
            this.imageSources[floder] = this.isResident(floder) ? new ImgResidentQueue() : new ImageSourceRefQueue(10);
            group = this.imageSources[floder];
        }

        let imageSource = group.get(filename);
        if (!imageSource) {
            ++this.imageTaskCount;
            let src = `/img/${floder}/${filename}.png`;
            let bitmap = await ResourceLoader.loadImageBitmap(src);
            imageSource = new ImageSource({ resource: bitmap, scaleMode: 'nearest' });
            imageSource.floder=floder;
            group.set(filename, imageSource);
            --this.imageTaskCount;
        }
        return imageSource;
    }

    static releaseUnknowImageSource(source){
        if(source&&source.label=='EMPTY')return;
        if(!source?.destroyed){
            let group=this.imageSources[source.floder];
            if(group){
                group.release?.(source.key);
            }else{
                source.destroy?.();
            }
        }
    }

    static releaseImageSource(floder, filename) {
        let group = this.imageSources[floder];
        if (!group) return;
        group.release?.(filename);
    }


    static async loadSystem(filename) {return await this.loadImageSource('system', filename);}
    static async loadResident(filename) {return await this.loadImageSource('resident', filename);}
    static async loadAnimation(filename) {return await this.loadImageSource('animations', filename);}
    static async loadBattleback1(filename) {return await this.loadImageSource('battlebacks1', filename);}
    static async loadBattleback2(filename) {return await this.loadImageSource('battlebacks2', filename);}
    static async loadEnemy(filename) {return await this.loadImageSource('enemies', filename);}
    static async loadCharacter(filename) {return await this.loadImageSource('characters', filename);}
    static async loadFace(filename) {return await this.loadImageSource('faces', filename);}
    static async loadParallax(filename) {return await this.loadImageSource('parallaxes', filename);}
    static async loadPicture(filename) {return await this.loadImageSource('pictures', filename);}
    static async loadSvActor(filename) {return await this.loadImageSource('sv_actors', filename);}
    static async loadSvEnemy(filename) {return await this.loadImageSource('sv_enemies', filename);}
    static async loadSystem(filename) {return await this.loadImageSource('system', filename);}
    static async loadTileset(filename) {return await this.loadImageSource('tilesets', filename);}
    static async loadTitle1(filename) {return await this.loadImageSource('titles1', filename);}
    static async loadTitle2(filename) {return await this.loadImageSource('titles2', filename);}

    static releaseSystem(filename) { this.releaseImageSource('system', filename); }
    static releaseResident(filename) { this.releaseImageSource('resident', filename); }
    static releaseAnimation(filename) { this.releaseImageSource('animations', filename); }
    static releaseBattleback1(filename) { this.releaseImageSource('battlebacks1', filename); }
    static releaseBattleback2(filename) { this.releaseImageSource('battlebacks2', filename); }
    static releaseEnemy(filename) { this.releaseImageSource('enemies', filename); }
    static releaseCharacter(filename) { this.releaseImageSource('characters', filename); }
    static releaseFace(filename) { this.releaseImageSource('faces', filename); }
    static releaseParallax(filename) { this.releaseImageSource('parallaxes', filename); }
    static releasePicture(filename) { this.releaseImageSource('pictures', filename); }
    static releaseSvActor(filename) { this.releaseImageSource('sv_actors', filename); }
    static releaseSvEnemy(filename) { this.releaseImageSource('sv_enemies', filename); }
    static releaseTileset(filename) { this.releaseImageSource('tilesets', filename); }
    static releaseTitle1(filename) { this.releaseImageSource('titles1', filename); }
    static releaseTitle2(filename) { this.releaseImageSource('titles2', filename); }

    static preloadSystem(){}

    static destroy() {
        if(this.imageSources){
            for (let groupKey in this.imageSources) {
                this.imageSources[groupKey]?.clear();
            }
            this.imageSources = null;
        }
    }
}
